﻿using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using GameLibrary.Model;
using GameLibrary.Model.Core;
using G.Model.Runtime;
using Microsoft.VisualBasic.PowerPacks;
using G.Runtime;

namespace GameDesigner.Source.Widgets
{
    public class ButtonPanel
    {
        private readonly GameDef _GameDef;
        private readonly SimpleShape _SimpleShape;
        private readonly List<ButtonWidget> _Widgets = new List<ButtonWidget>();

        public ButtonPanel(Game game, SimpleShape control)
        {
            _GameDef = game.GameDef;
            _SimpleShape = control;
            game.UserObjects[this.GetType().Name] = this;
            game.Elements.ElementMovedEvent += OnElementMovedEvent;
        }

        public ButtonWidget Add(Game pGame, GameTask pGameTask, GameElement pGameElement)
        {
            ButtonWidget w = new ButtonWidget(pGameElement, this, 
                _SimpleShape, pGame, pGameTask, pGameTask.Def.Name);
            w.Point = new Point(5, 5 + (_Widgets.Count * 30));
            _Widgets.Add(w);
            _SimpleShape.Refresh();
            return w;
        }

        public void Clear()
        {
            List<ButtonWidget> widgets = new List<ButtonWidget>(_Widgets);
            foreach (ButtonWidget widget in widgets)
            {
                Remove(widget);
            }
        }

        void OnElementMovedEvent(GameElement element)
        {
            Clear();
        }

        public void Remove(ButtonWidget button)
        {
            _Widgets.Remove(button);
            button.Dispose();
            _SimpleShape.Refresh();
        }

        public IEnumerable<ButtonWidget> ButtonWidgets 
        {
            get { return _Widgets; }
        }

        public void Refresh()
        {
        }

        public void OnPaint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.LightGray, 1);
            foreach (ButtonWidget w in _Widgets)
            {
                w.Update(g);
            }
        }
    }
}
